//
//  XSLTransitionAnimation.m
//  Tvvvvv
//
//  Created by xingshuai on 2020/12/16.
//

#import "XSLTransitionAnimation.h"

/** 屏幕大小 */
#define ScreenHeight [UIScreen mainScreen].bounds.size.height             // 屏幕宽
#define ScreenWidth  [UIScreen mainScreen].bounds.size.width              // 屏幕高

@implementation XSLTransitionAnimation

//初始位置
-(void)setCurRect:(CGRect)curRect {
    _curRect = curRect;
}

//返回动画事件
- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext {
    return 0.5;
}

//所有的过渡动画事务都在这个方法里面完成
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext {
    switch (_transitionType) {
        case XSLTransitionTypePresent:
            [self presentAnimation:transitionContext];
            break;
        case XSLTransitionTypeDissmiss:
            [self dismissAnimation:transitionContext];
            break;
    }
}

#pragma mark -- transitionType
- (void)presentAnimation:(id<UIViewControllerContextTransitioning>)transitionContext {
    // 取出转场后视图控制器上的视图view
    UIView * toView = [transitionContext viewForKey:UITransitionContextToViewKey];
    // 再得到过渡的容器View 用于在动画中呈现出toView
    UIView * containerView = [transitionContext containerView];
    //添加到containerView中
    [containerView addSubview:toView];
    // toView初始位置大小
    toView.frame = self.curRect;
    // 全屏展示
    [UIView animateWithDuration:0.3 animations:^{
        toView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
        [toView layoutIfNeeded];
    } completion:^(BOOL finished) {
        // 动画结束 转场结束
        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
    }];
}

- (void)dismissAnimation:(id<UIViewControllerContextTransitioning>)transitionContext {
    UIViewController * fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIView *fromView = fromVC.view;
    //执行的dismiss动画
    [UIView animateWithDuration:0.3 animations:^{
        fromView.frame = self.curRect;
        [fromView layoutIfNeeded];
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
    }];
}



@end
